package com.slipper.core.message;

import com.slipper.util.Tool;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class MessageUtil {
    private static Publisher publisher;
    private static boolean subscribeLogger = false;

    @Resource
    public void setPublisher(Publisher publisher) {
        MessageUtil.publisher = publisher;
    }

    public static void subscribe(String topic, Subscriber subscriber) {
        if (Tool.isBlank(topic) || subscriber == null) return;
        publisher.subscribe(topic, subscriber);
    }

    public static void unsubscribe(Subscriber subscriber) {
        if (subscriber == null) return;
        publisher.unsubscribe(subscriber);
    }

    public static void publish(String topic) {
        publish(topic, null);
    }

    public static void publish(String topic, String message) {
        if (Tool.isBlank(topic)) return;
        publisher.publish(topic, message);
    }

    public static void setSubscribeLogger(boolean subscribeLogger) {
        MessageUtil.subscribeLogger = subscribeLogger;
    }

    public static boolean isSubscribeLogger() {
        return MessageUtil.subscribeLogger;
    }

    public static void publishLogger(String message) {
        publisher.publish(Topic.LOGGER, message);
    }
}
